package models

type Message struct {
	Model
	Key 	string 	`gorm:"unque_index;not_null;"json:"key"`
	Content string 	`json:"content"`
	User    User 	`json:"user"`
	UserId 	int 	`json:"user_id"`
	NoteKey string 	`json:"note_key"`
	Praise 	int 	`gorm:"default:0;"json:"praise"`
}

func SaveMessage(message *Message) error{
	return db.Save(message).Error
}

func QueryMessageByNoteKey(key string)(ms []*Message,err error){
	return ms,db.Debug().Preload("User").Where("note_key = ?",key).Order("updated_at desc").Find(&ms).Error
}

func QueryNotesCountByNoteKey(notekey string)(count int,err error){
	return count,db.Model(&Message{}).Where("note_key = ?",notekey).Count(&count).Error
}

func QueryPageMessageByNoteKey(notekey string,pageno int,pagesize int)(ms []*Message,err error){
	return ms,db.Debug().Preload("User").Where("note_key = ?",notekey).Offset((pageno-1)*pagesize).Limit(pagesize).Order("updated_at desc").Find(&ms).Error
}